package com.conversationboard.controller;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.conversationboard.cache.BannedUserCache;
import com.conversationboard.config.Configuration;
import com.conversationboard.controller.ipaddress.IPAddress;
import com.conversationboard.model.Message;
import com.conversationboard.model.NoSuchThreadException;
import com.conversationboard.model.Thread;
import com.conversationboard.model.User;
import com.conversationboard.model.banrequests.BanRequests;

@WebServlet(name = "VoteForBanControllerServlet", urlPatterns = "/VoteForBanControllerServlet")
public class VoteForBanControllerServlet extends HttpServlet {

	private static final long serialVersionUID = 7783764230303375806L;


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		try {

			if (!Configuration.getInstance().isSupportSuggestBan()) {
				return;
			}

			User user = User.get(request.getUserPrincipal().getName());

			if (!SecurityChecks.passes(request, user, this)) {
				return;
			}

			/*
			 * If this user is banned from any board, don't let them suggest
			 * bans
			 */

			if (BannedUserCache.isBanned(-1, false, user.getLoginId())) {
				return;
			}

			int boardId = Integer.parseInt(request.getParameter("boardid"));
			int threadId = Integer.parseInt(request.getParameter("threadid"));
			int messageId = Integer.parseInt(request.getParameter("messageid"));

			String bannerLoginId = user.getLoginId();
			String ipAddress = IPAddress.getIPAddress(request);
			String toBeBannedLoginId = User.getLoginIdFromMessageId(boardId, threadId, messageId);
			Thread thread = Thread.get(boardId, threadId);
			Message message = thread.getMessage(messageId);

			/* Check that user has not voted for this person to be banned before */

			if (BanRequests.hasAlreadyRequestedBan(bannerLoginId, toBeBannedLoginId)) {
				response.sendRedirect(Configuration.getInstance().getRoot() + "/ThreadSelectedControllerServlet?boardid=" + boardId + "&threadid=" + threadId);
				return;
			}

			/*
			 * Increment the ban count on the user roles table, if the user
			 * isn't an admin. Note that this servlet does not actually perform
			 * banning; this is done in batch.
			 */

			User toBeBannedUser = User.get(toBeBannedLoginId);

			// Uncomment to stop admins being SBd
			// if (!toBeBannedUser.isBoardAdmin(boardId)) {

			BanRequests.addBanRequest(bannerLoginId, ipAddress, toBeBannedLoginId, boardId, threadId, messageId, message.getBody());
			toBeBannedUser.setSuggestBans(toBeBannedUser.getSuggestBans() + 1);

			response.sendRedirect(Configuration.getInstance().getRoot() + "/ThreadSelectedControllerServlet?boardid=" + boardId + "&threadid=" + threadId);

		} catch (SQLException e) {
			throw new ServletException(e);
		} catch (NoSuchThreadException e) {
			throw new ServletException(e);
		}
	}

}
